package com.cskaoyan;

import com.cskaoyan.bean.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 创建日期: 2022/05/19 11:13
 *
 * @author ciggar
 */
public class MybatisDemo {

    public static void main(String[] args) throws IOException {

        // 目的：要去执行sql语句

        // 1. 读取配置文件
        InputStream inputStream = MybatisDemo.class.getClassLoader().getResourceAsStream("mybatis-config.xml");

        InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");


        // 2. 获取SqlSessionFactory
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(stream);


        // 3. 执行SQL语句
        SqlSession sqlSession = sqlSessionFactory.openSession();

          // 需要传入Sql语句的坐标  = namespace . id
        Account account = sqlSession.selectOne("cskaoyan.selectAccountById", 2);

        List<Account> list = sqlSession.selectOne("cskaoyan.selectAccountList");


        System.out.println(account);
        System.out.println(list);


    }

}
